package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class BibliotecaApp {


    private static final String WELCOME_TO_BANGALORE_PUBLIC_LIBRARY = "Welcome to Bangalore Public Library";
    private BufferedReader reader;
    private PrintStream printStream;
    private Collection<String> menuOptions = Arrays.asList("Enter 1 to view the list of all books.", "Enter 2 to reserve books.",
            "Enter 3 to check your membership details.", "Enter 4 to view all movies.");
    private Map<String, Command> commandMap;


    public BibliotecaApp(BufferedReader reader, PrintStream ps, Map<String, Command> commandMap) {
        this.reader = reader;
        this.printStream = ps;
        this.commandMap = commandMap;

    }

    public static void main(String[] args) throws IOException {
        Map<String, Command> commandMap = new HashMap<String, Command>();
        commandMap.put("1", new PrintBooksCommand());
        commandMap.put("2", new ReserveBookCommand());
        commandMap.put("3", new DisplayMembershipDetailsCommand());
        commandMap.put("4", new PrintMoviesCommand());
        commandMap.put("Invalid", new InvalidOptionsCommand());
        BibliotecaApp bibliotecaApp = new BibliotecaApp(new BufferedReader(new InputStreamReader(System.in)),
                System.out, commandMap);
        bibliotecaApp.printWelcomeMessage();
        bibliotecaApp.startApp();
    }

    public void startApp() throws IOException {
        Menu mainMenu = new Menu(printStream, reader, menuOptions);
        String menuChoice = mainMenu.promptUserInput().trim();
        if(commandMap.keySet().contains(menuChoice)){
            commandMap.get(menuChoice).execute();
        }else{
            commandMap.get("Invalid").execute();
        }
    }

    public void printWelcomeMessage(){
        printStream.println(WELCOME_TO_BANGALORE_PUBLIC_LIBRARY);
    }

}
